<?php 
require_once('dao/OffreFixeDao.php');
require_once('dao/OffreAchatFixeDao.php');
require_once('dao/OffreVenteOptionFixeDao.php');

if ($_SESSION['niveauAuth']=='1'or $_SESSION['niveauAuth']=='3' or $_SESSION['niveauAuth']=='4'or $_SESSION['niveauAuth']=='7'or $_SESSION['niveauAuth']=='8') {
/**
 * Controleur pour la creation d'offre 
 */
 
// initialise les variables
$choix_offre = $offre_choisie = $options_incluses = $options = NULL; 

//récupération du type (vga, vga/num, voip ou xdsl)
$type = $_GET['type'];

//liste des ha
if($_GET['type'] == 'vga' or $_GET['type'] == 'vga num')
{
	$offres = OffreAchatFixeDao::getAllVga();
}elseif($_GET['type'] == 'voip'){
	$offres = OffreAchatFixeDao::getAllVoip();
}elseif($_GET['type'] == 'xdsl'){
	$offres = OffreAchatFixeDao::getAllXdsl();
}

// si un choix d'offre ha a été effectué
if(isset($_POST['choix_offre'])){ 
	$choix_offre = $_POST['choix_offre'];
	$offre_choisie = OffreAchatFixeDao::searchById($choix_offre);
	//var_dump($offre_choisie);
	$options_incluses = $offre_choisie[0]['options'];

	//récupération des options
	foreach(explode(",",$options_incluses) as $key => $option)
	{
		$options_details = OffreVenteOptionFixeDao::searchById($option);
		if(isset($options_details[0]['nom'])){$options[] = $options_details[0]['nom'];} else {$options = array();}
	}
}	

//liste des pays 
if($_GET['type'] == 'vga' or $_GET['type'] == 'vga num')
{
$pays = OffreFixeDao::getAllPaysVga();	
}elseif($_GET['type'] == 'voip'){
$pays = OffreFixeDao::getAllPaysVoip();	
}elseif($_GET['type'] == 'xdsl'){
$pays = NULL;
}
	
//création d'une nouvelle offre voip
if (isset($_POST['creation']))
{
	$code_article = $_POST['code_article'];
	$nom = $_POST['nom'];
	$type = $_POST['type'];
	$options_incluses = $_POST['options_incluses'];
	$temps_min = $_POST['temps_min'];
	$jours = $_POST['jours'];
	$horaires = $_POST['horaires'];
	if(isset($_POST['fixe'])){$fixe = $_POST['fixe'];}else{$fixe = NULL;}
	if(isset($_POST['mobile'])){$mobile = $_POST['mobile'];}else{$mobile = NULL;}
	$international = $_POST['pays'];
	if(isset($_POST['interne'])){$interne = $_POST['interne'];}else{$interne = NULL;}
	$pv0m = $_POST['pv0m'];
	$pv12m = $_POST['pv12m'];
	$pv24m = $_POST['pv24m'];
	$pv36m = $_POST['pv36m'];
	$pv48m = $_POST['pv48m'];
	$id_ha = $_POST['id_ha'];
	
	$requete = OffreFixeDao::create('',$code_article, $nom, $type, $options_incluses, $temps_min, $jours, $horaires,$fixe,$mobile,$international, $interne, $pv0m, $pv12m, $pv24m, $pv36m, $pv48m, $id_ha);
	
	// Redirection vers la page 
	if ($_POST['action'] == "ve-offre-fixe-creation"){
		header("Location:ve-offre-fixe-creation.php?success=true");
	} elseif ($_POST['action'] == "ve-offre-fixe"){
		header("Location:ve-offre-fixe.php?success=true");
	}
}

//$twig = new Twig_Environment($loader, array(
 //   'debug' => true,
//)); 
//$twig->addExtension(new Twig_Extension_Debug());
// a afficher dans le html: {{ debug() }}

echo $twig->render('ve-offre-fixe-creation.html',
		array(	'type' => $type,
				'offres' => $offres,
				'options' => $options, 
				'options_incluses' => $options_incluses,
				'choix_offre' => $choix_offre, 
				'offre_choisie' => $offre_choisie,
				'pays' => $pays,
				'message' => $message, 
				'error' => $error));
}
else {
	redirect("home-admin");
}
